<?php

require_once realpath (dirname (__FILE__) . '//..//..//pages.php');
require_once CLASS_TAG;

/**
 * Description of FullTag.
 *
 * @author Andrea Cantafio
 */
class FullTag extends Tag {

    /**
     *
     * @var boolean if set true __toString method will return normal representation of this tag else it will return only the opening of this tag
     */
    protected $auto_close = true;
    protected $auto_new_line = true;
    protected $auto_tab = true;
    protected $parseHTML = true;

    /**
     *
     * @var array contains all the HTML code that will be included before cloding tag.
     */
    protected $innerHTML = array ();

    public function __construct ($name, $attributes = null, $innerHTML = null, $outerHTML = null) {
        parent::__construct ($name, $attributes);
        if (isset ($innerHTML)) {
            if ($this->parseHTML) {
                $innerHTML = $this->parseHTML ($innerHTML);
            }
            $this->innerHTML = $innerHTML;
        }
        if (isset ($outerHTML) && is_a ($outerHTML, 'Tag')) {
            $this->outerHTML = $outerHTML;
        }
    }

    /**
     * This method returns a reprsentation of this tag.
     * 
     * Returns 
     * 
     * @return string representation of this tag 
     */
    public function __toString () {
        $str = parent::__toString () . ">";
        /* if ($this->auto_new_line) {
          $str .= "\n";
          }
          if ($this->auto_tab) {
          $str .= "\t";
          } */
        if ($this->auto_close) {

            if ($this->parseHTML) {
                $str .= $this->join ($this->innerHTML);
            } else {
                $str .= $this->innerHTML;
            }

            $str .= "</$this->name>";
            if ($this->auto_new_line) {
                $str .= "\n";
            }
            if ($this->auto_tab) {
                $str .= "\t";
            }
        }
        return $str;
    }

    /**
     * Adds <code>$tag</code> content to this tag
     * @param string|Tag $html HTML code that will be added within this tag|Tag object (or one of its child) that will be added within this tag
     * @return boolean true if <code>$tag</code> is a string or a Tag object (or one of its child)
     */
    public function add ($html) {
        if (is_a ($html, 'Tag')) {
            $html->anchestor = $this->anchestor;
            $html->outerHTML = $this;
            $this->names = array_merge_recursive ($this->names, $html->names);
            $this->ids = array_merge_recursive ($this->ids, $html->ids);
            $this->tags = array_merge_recursive($this->tags, $html->tags);
            $this->tags[$html->name][] = $html;
            $this->innerHTML[] = $html;
            return true;
        } else if (is_array ($html) || is_string ($html)) {
            $this->innerHTML = $this->parseHTML ($html);
        }
        return false;
    }
    
    public function emptyInnerHTML () {
        $this->innerHTML = array ();
    }

    /**
     * 
     * @return array 
     */
    public function getInnerHTML ($index = null) {
        if (isset ($index) && is_string ($index) && !empty ($index)) {
            if (isset ($this->innerHTML[$index])) {
                return $this->innerHTML[$index];
            } else {
                return false;
            }
        }
        return $this->innerHTML;
    }

    public function getSize () {
        return $this->index;
    }

    private function join ($array) {
        $str = '';
        foreach ($array as $html) {
            if (is_a ($html, 'Tag')) {
                $str .= $html->__toString ();
            } else if (is_array ($html)) {
                $str .= join ($html);
            } else if (is_string ($html)) {
                $str .= $html;
            }
        }
        return $str;
    }

    public function setInnerHTML ($index, $html) {
        $this->innerHTML[$index] = $this->parseHTML ($html);
    }

}

?>
